home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / T / TC Prog Guide.cpt / picture ƒ / project.c < prev    next >
Text File  |  1991-02-16  |  6KB  |  199 lines

  1. /*
  2. *    FILE:        project.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    October 3, 1990
  5. *
  6. *    methods for projector class, which displays 2D images on screen
  7. */
  8.  
  9. # include    "project.h"
  10. # include    "error.h"
  11. # include    <stdlib.h>
  12.  
  13. extern Error    *gerror;
  14. static int        old_window_num = -1;
  15. static double    old_c2_x;
  16. static double    old_c2_y;
  17.  
  18. /******************************************************************
  19. *    initialize.  Be sure to call inherited method first if
  20. *    overriding.
  21. ******************************************************************/
  22. boolean    Projector::init(void)
  23. {
  24.     cropping_frame = new(Frame);
  25.     cropping_frame->init();
  26.     projection_frame = new(Frame);
  27.     projection_frame->init();
  28.     window_num = -1;
  29.     window_frame = NULL;
  30.     screen_ptr = NULL;
  31.     
  32. /*    derived projectors may change following initializations: */
  33.  
  34.     set_cropping_frame(0.,0.,1.,1.);
  35.     set_projection_frame(0.,0.,1.,.1);
  36.     set_background_color(BLACK);
  37.     
  38.     return TRUE;
  39. }
  40.  
  41. /******************************************************************
  42. *    set background color - duh!  (Default is set in init().)
  43. ******************************************************************/
  44. void    Projector::set_background_color(color bck_color_val)
  45. {
  46.     background_color = bck_color_val;
  47. }
  48.  
  49. /******************************************************************
  50. *    set cropping frame to indicate what part of 2D space will
  51. *    be drawn.  Otherwise the default will be used.  In 3D case,
  52. *    use coordinates of focal plane of camera.
  53. ******************************************************************/
  54. void    Projector::set_cropping_frame(double x,double y,
  55.                                     double width,double height)
  56. {
  57.     cropping_frame->set(x,y,width,height);
  58. }
  59.  
  60. /******************************************************************
  61. *    set projection frame to indicate where the window will appear
  62. *    on the screen.  This must be called before set_screen(), if
  63. *    at all!  Use normalized screen coordinates.
  64. ******************************************************************/
  65. void    Projector::set_projection_frame(double x,double y,
  66.                                     double width,double height)
  67. {
  68.     projection_frame->set(x,y,width,height);
  69. }
  70.  
  71. /******************************************************************
  72. *    set pointer to screen used for projection, and allocate a
  73. *    window on the screen.  This must be called before clearing
  74. *    or drawing!  Do not call this more than once!
  75. ******************************************************************/
  76. void    Projector::set_screen(Screen* screen_ptr_val)
  77. {
  78.     if (screen_ptr != NULL)
  79.         gerror->report("Screen already set");
  80.     else
  81.     {
  82.         screen_ptr = screen_ptr_val;
  83.         window_num = screen_ptr->new_window(projection_frame);
  84.         window_frame = new(Frame);
  85.         window_frame->init();
  86.         screen_ptr->get_window_device_frame(window_num,window_frame);
  87.         clear();
  88.     }
  89. }
  90.  
  91. /******************************************************************
  92. *    clear window using background color
  93. ******************************************************************/
  94. void    Projector::clear(void)
  95. {
  96.     if (screen_ptr == NULL)
  97.         gerror->report("Can't clear() with no screen");
  98.     else
  99.     {
  100.         screen_ptr->set_current_window(window_num);
  101.         screen_ptr->set_pen_color(background_color);
  102.         screen_ptr->fill_window();
  103.         old_window_num = -1;
  104.     }
  105. }
  106.  
  107. /******************************************************************
  108. *    make corresponding window closest
  109. ******************************************************************/
  110. void    Projector::overlap(void)
  111. {
  112.     if (screen_ptr == NULL)
  113.         gerror->report("Can't overlap() with no screen");
  114.     else
  115.         screen_ptr->make_closest(window_num);
  116. }
  117.  
  118. /******************************************************************
  119. *    show line on screen.  If the window hasn't changed and the
  120. *    new line continues where the previous one left off, then
  121. *    drawing may be faster.  2D world coordinates are used (in 3D,
  122. *    those of camera focal plane).
  123. ******************************************************************/
  124. void    Projector::show_line(Coord2 *c1,Coord2 *c2,color line_color)
  125. {
  126.     Coord2    *c3,
  127.             *c4;
  128.             
  129.     if (screen_ptr == NULL)
  130.         gerror->report("Can't show_line() with no screen");
  131.     else
  132.     {
  133.         if (old_window_num == window_num &&
  134.             old_c2_x == c1->x && old_c2_y == c1->y)
  135.         {
  136.             screen_ptr->set_pen_color(line_color);
  137.             c4 = new(Coord2);
  138.             c4->init();
  139.             c4->convert(c2,cropping_frame,window_frame);
  140.             screen_ptr->draw_to(c4);
  141.             c4->destroy();
  142.             delete(c4);
  143.         }
  144.         else
  145.         {
  146.             screen_ptr->set_current_window(window_num);
  147.             screen_ptr->set_pen_color(line_color);
  148.             c3 = new(Coord2);
  149.             c3->init();
  150.             c3->convert(c1,cropping_frame,window_frame);
  151.             c4 = new(Coord2);
  152.             c4->init();
  153.             c4->convert(c2,cropping_frame,window_frame);
  154.             screen_ptr->draw_line(c3,c4);
  155.             c3->destroy();
  156.             delete(c3);
  157.             c4->destroy();
  158.             delete(c4);
  159.         }
  160.         
  161.         old_window_num = window_num;
  162.         old_c2_x = c2->x;
  163.         old_c2_y = c2->y;
  164.     }
  165. }
  166.  
  167. /******************************************************************
  168. *    destroy
  169. ******************************************************************/
  170. boolean    Projector::destroy(void)
  171. {
  172.     cropping_frame->destroy();
  173.     delete(cropping_frame);
  174.     projection_frame->destroy();
  175.     delete(projection_frame);
  176.     if (window_frame != NULL)
  177.     {
  178.         window_frame->destroy();
  179.         delete(window_frame);
  180.     }
  181.     
  182.     return TRUE;
  183. }
  184.  
  185. /******************************************************************
  186. *    initialize corner projector
  187. ******************************************************************/
  188. boolean    Corner_Projector::init(void)
  189. {
  190.     Projector::init();
  191.     
  192.     set_cropping_frame(0.,0.,2.,2.);
  193.     set_projection_frame(.6,-.4,.4,.4);
  194.     set_background_color(MAGENTA);
  195.     
  196.     return TRUE;
  197. }
  198.  
  199.